package com.exp.bookhouse.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exp.bookhouse.pojo.Permission;
import com.exp.bookhouse.service.PermissionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Api(value = "资源权限管理接口",tags = "管理员管理资源权限操作")
@Controller
@RequestMapping("/admin")
public class PermissionControllerAdmin {

    @Autowired
    private PermissionService permissionService;


    @ApiOperation(value = "查询资源权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "p", value = "页数", required = false, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "s", value = "显示记录数", required = false, dataType = "Int"),
    })
    @GetMapping("/toPermission")
    public String toPermission(@RequestParam(value = "p", required = false, defaultValue = "1")Integer page,
                               @RequestParam(value = "s", required = false, defaultValue ="5")Integer size,
                               Model model){
        IPage<Permission> iPage = permissionService.queryPermissionsByPage(page,size);
        model.addAttribute("iPage",iPage);
        return "permission";
    }


    @GetMapping("/toSavePermission")
    public String toSaveRole(){
        return "permissionAdd";
    }


    @ApiOperation(value = "新增权限")
    @PostMapping("/savePermission")
    public String savePermission(@ApiParam(value = "权限信息", required = true) Permission permission){
        int count = permissionService.savePermission(permission);
        return "redirect:/admin/toPermission";
    }

    @ApiOperation(value = "根据id查询权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "编号", required = true, dataType = "Long")
    })
    @GetMapping("/toUpdatePermission/{id}")
    public String toUpdatePermission(@PathVariable("id")Long id, Model model){
        Permission permission = permissionService.queryPermissionById(id);
        model.addAttribute("permission",permission);
        return "permissionUpdate";
    }

    @ApiOperation(value = "更新权限")
    @PostMapping("/updatePermission")
    public String updatePermission(@ApiParam(value = "权限信息",required = true) Permission permission){
        int count = permissionService.updatePermission(permission);
        return "redirect:/admin/toPermission";
    }

    @ApiOperation(value = "删除权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "编号id", required = true, dataType = "Long")
    })
    @GetMapping("/deletePermission/{id}")
    public String deletePermission(@PathVariable("id")Long id) throws Exception {
        permissionService.deletePermission(id);
        return "redirect:/admin/toPermission";
    }


}
